<?php
/**
 * +------------------------------------------------------------+
 * @category IndexAction
 * +------------------------------------------------------------+
 * 健康图表
 * +------------------------------------------------------------+
 *
 * @author funwee@qq.com
 * @copyright http://www.suncco.com 2013
 * @version 1.0
 *
 * Modified at : 2013-10-22 上午10:51:44
 *
 */
class IndexAction extends Action{

	public function index(){
		$this->display();
	}
	
	/**
	 * 获取状态图表数据
	 */
	public function getHealthData(){
		$range = $_POST['range'];
		$weekstart = mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y"));
		$weekend = mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"));
		$monthstart = mktime(0, 0 , 0,date("m"),1,date("Y"));
		$monthend = mktime(23,59,59,date("m"),date("t"),date("Y"));
		
		$cond['uid'] = $this->mid;
		$model = D('Mini');
		if($range){
			switch ($range){
				case 'week': $cond['cTime'] = array(array('gt',$weekstart),array('lt',$weekend)); break;
				case 'month': $cond['cTime'] = array(array('gt',$monthstart),array('lt',$monthend)); break;
			}
			$data = $model->where($cond)->order('cTime desc')->select();
//			dump($model->getLastSql());exit;
		}else{
			$data = $model->where($cond)->order('cTime desc')->limit(10)->select();
		}
		krsort($data);//根据键名逆向排序
		if($data){
			foreach($data as $key=>$rs){
				$result['date'][] = date('m-d',$rs['cTime']);
				$result['result'][] = array('y'=>(int)$rs['status'], 'content'=>$rs['content']);
			}
			$this->ajaxReturn($result, 'success', 1);
		}else{
			$this->ajaxReturn(null, 'error', 0);
		}
	}
	
	/**
	 * 添加状态
	 */
	public function addMini(){
        $appconfig	=	D('AppConfig');
        $appconfig->setAppname('mini');
        $config	=	$appconfig->getConfig();
        $this->assign( 'stringcount',$config['stringcount'] );		
		$this->display();
	}
	
	public function getBMI(){
		$height = $_POST['height'];
		$weight = $_POST['weight'];
		if($height && $weight){
			$data['BMI'] = round($weight/(pow($height/100,2)),2);
			$weight_bmi_19 = round(19 * pow($height/100,2));
			$weight_bmi_25 = round(25 * pow($height/100,2));
			$data['normal'] = $weight_bmi_19.'~'.$weight_bmi_25;
			
			if($data){
				$this->ajaxReturn($data, 'success', 1);
			}else{
				$this->ajaxReturn(null, 'error', 0);
			}
		}
		
	}
}
?>